package com.sisy.demo.vo;

import org.springframework.validation.BindingResult;

/**
 * @Description: 项目返回对象
 * @author: xx
 * @date: 2020-06-05 16:01
 * @version: 0.0.1
 * @Copyright: 2020 www.jztdata.com Inc. All rights reserved.
 * 注意：本内容仅限于杭州金智塔科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class S2VJSONResult<T> {

    private Integer code;//状态码
    private Boolean isSuccess;//状态
    private String message;//消息
    private T data;//数据对象
    /**
     * 无参构造器
     */
    public S2VJSONResult(){
        super();
    }

    public static <T> S2VJSONResult<T> all(Boolean success, Integer code,String message,T data) {
        return new S2VJSONResult(success, code, message,  data);
    }

    public static S2VJSONResult msg(Boolean success, Integer code,String message) {
        return new S2VJSONResult(success, code, message);
    }

    public static S2VJSONResult clientError(BindingResult bindingResult){
        return S2VJSONResult.msg(false, 400, bindingResult.getFieldError().getDefaultMessage());
    }

    public static S2VJSONResult clientError(String errorMsg) {
        return S2VJSONResult.msg(false, 400, errorMsg);
    }

    public static <T> S2VJSONResult<T> data(Boolean success, Integer code,T data) {
        return new S2VJSONResult(success, code, data);
    }

    public static <T> S2VJSONResult<T> ok(T data) {
        return new S2VJSONResult(data);
    }


    public static S2VJSONResult ok() {
        return new S2VJSONResult(null);
    }


    /**
     * 只返回状态，状态码，消息
     * @param success
     * @param code
     * @param message
     */
    public S2VJSONResult(Boolean success, Integer code, String message) {
        super();
        this.code = code;
        this.message = message;
        this.isSuccess=success;
    }

    /**
     * 只返回状态，状态码，数据对象
     * @param success
     * @param code
     * @param data
     */
    public S2VJSONResult(Boolean success, Integer code, T data){
        super();
        this.isSuccess=success;
        this.code=code;
        this.data=data;
    }

    /**
     * 返回全部信息即状态，状态码，消息，数据对象
     * @param success
     * @param code
     * @param message
     * @param data
     */
    public S2VJSONResult(Boolean success, Integer code, String message, T data){
        super();
        this.isSuccess=success;
        this.code=code;
        this.message=message;
        this.data=data;
    }


    public S2VJSONResult(T data) {
        this.code = 200;
        this.message = "OK";
        this.data = data;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getIsSuccess() {
        return isSuccess;
    }

    public void setIsSuccess(Boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
